<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use App\Notifications\ResetPasswordNotification;

class Sms extends Authenticatable
{

    use HasApiTokens,Notifiable;


    protected $table = 'sms';
    protected $fillable = ['type','code','status','expire_time','create_time','phone'];

    public    $timestamps = false;
    protected $primaryKey = 'id';

    public function checkCode($verifycode,$phone,$type=1){
        $where = [
          'phone'=>$phone,
          'code'=>$verifycode,
          'type'=>$type,
          'status'=>0,
        ];

        $data = DB::table('sms')->where($where)->where('expire_time','>=',date('Y-m-d H:i:s'))->orderBy('id','desc')->first(['id']);
        if($data){
            DB::table('sms')->where(['id'=>$data->id])->update(['status'=>1]);
            return true;
        }else{
            return false;
        }
    }
}
